package com.spiro;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.data.Stat;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by Administrator on 2016/10/11 0011.
 */
public class ApiTest {

    private ZooKeeper zooKeeper;

    public ApiTest(String connectString) throws IOException {
        this.zooKeeper = new ZooKeeper(connectString, 40000, new Watcher() {
            public void process(WatchedEvent event) {

                if (event.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("ZOOKEEPER CONNECTED, sessionId -> " + zooKeeper.getSessionId()
                            + " timeout -> " + zooKeeper.getSessionTimeout());
                }

                System.out.println(event);
            }
        });

        System.out.println("New ApiTest");
    }

    public String testCreate(String path, CreateMode createMode) throws KeeperException, InterruptedException {
        return zooKeeper.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
    }

    public void testExists(String path, boolean watch) throws KeeperException, InterruptedException {
        Stat stat = zooKeeper.exists(path, watch);
        System.out.println(stat);
    }

    public void testExistWatch(final String path) {

        final String ctx = "I am ctx!";

        final AsyncCallback.StatCallback callback = new AsyncCallback.StatCallback() {
            public void processResult(int rc, String path, Object ctx, Stat stat) {
                System.out.println("rc -> " + rc + ", path -> "
                        + path + ", ctx -> " + ctx + ", stat -> " + stat);
            }
        };

        Watcher watcher = new Watcher() {
            public void process(WatchedEvent event) {
                System.out.println("Exists watch event -> " + event.toString());
                zooKeeper.exists(path, this, callback, ctx);
            }
        };

        zooKeeper.exists(path, watcher, callback, ctx);
        System.out.println("test exists Watch invoked.");
    }

    public void testSetData(String path, String data, int version) throws KeeperException, InterruptedException {
        zooKeeper.setData(path, data.getBytes(), version);
    }

    public void testGetData(String path) throws KeeperException, InterruptedException {
        Stat stat = new Stat();
        byte[] data = zooKeeper.getData(path, false, stat);
        System.out.println("data -> " + new String(data) + ", stat -> " + stat);
    }

    public void testDelete(String path) throws KeeperException, InterruptedException {
        List<String> children = zooKeeper.getChildren(path, false);
        for (String child : children) {
            testDelete(path + "/" + child);
        }
        zooKeeper.delete(path, -1);
        System.out.println("DELETE PATH -> " + path);
    }

    public void testSync(String path) {
        zooKeeper.sync(path, new AsyncCallback.VoidCallback() {
            public void processResult(int rc, String path, Object ctx) {
                System.out.println("rc -> " + rc + ", path -> " + path + ", ctx -> " + ctx);
            }
        }, "I am context!");
    }

    public void close() {
        if (this.zooKeeper != null) {
            try {
                this.zooKeeper.close();
                System.out.println("ZOOKEEPER CONNECTION CLOSED.");
            } catch (InterruptedException e) {
            }
        }
    }

    public static void main(String[] args) throws IOException {
        ApiTest test = null;

        try {
            test = new ApiTest("192.168.103.108:2181");

            Scanner scanner = new Scanner(System.in);
            if (scanner.next() != null) {
            }
            System.out.println("1");
            String path = test.testCreate("/haha", CreateMode.PERSISTENT);
            System.out.println(path);

            if (scanner.next() != null) {
            }
            System.out.println("2");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (KeeperException e) {
            e.printStackTrace();
        } finally {
            test.close();
        }
    }
}
